package com.txzt.gaea.provide.service.impl;


import com.alibaba.fastjson.JSON;
import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.api.client.AdminUserRemoteService;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.enums.ProjectFileTypeEnum;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.page.PageInfoBT;
import com.txzt.gaea.dto.UserSingleDetailQueryDTO;
import com.txzt.gaea.provide.dto.FileInfoDTO;
import com.txzt.gaea.provide.model.FileInfo;
import com.txzt.gaea.provide.service.FileInfoService;
import com.txzt.gaea.provide.vo.FileInfoVo;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;


@Service
@Log4j2
public class FileInfoServiceImpl extends BaseServiceImpl<FileInfo> implements FileInfoService {

    @Autowired
    private AdminUserRemoteService adminUserRemoteService;

    @Override
    public boolean fileChange(List<FileInfoVo> oldList, List<FileInfoVo> newList, String projectNo, Integer fileType){
        if (null != oldList){
            Collection<FileInfoVo> exists = new ArrayList<>(oldList);
            //需删除的
            exists.removeAll(newList);
            exists.forEach(item -> {
                FileInfo fileInfo = new FileInfo();
                fileInfo.setId(item.getFileId());
                fileInfo.setIsDelete(IsDeleteEnum.DELETE.getCode());
                this.sqlManager.updateTemplateById(fileInfo);
            });

        }
        newList.forEach(item -> {
            FileInfo fileInfo = new FileInfo();
            fileInfo.setId(item.getFileId());
            fileInfo.setProjectNo(projectNo);
            fileInfo.setBizType(fileType);
            this.sqlManager.updateTemplateById(fileInfo);
        });
        return true;
    }


    @Override
    public void fileChange(String oldFileJson, String newFileJson, String projectNo, Integer fileType) {
        List<FileInfoVo> oldList = new ArrayList<>();
        List<FileInfoVo> newList = new ArrayList<>();
        if (StringUtils.isNotEmpty(oldFileJson)) {
            oldList = JSON.parseArray(oldFileJson, FileInfoVo.class);
        }
        if (StringUtils.isNotEmpty(newFileJson)) {
            newList = JSON.parseArray(newFileJson, FileInfoVo.class);
        }
        //取交集
        List<FileInfoVo> intersection = new ArrayList<>(oldList);
        intersection.retainAll(newList);
        //oldList对newList的相对补集
        oldList.removeAll(intersection);
        //newList对oldList的相对补集
        newList.removeAll(intersection);
        //需删除的文件
        oldList.forEach(item -> {
            FileInfo fileInfo = new FileInfo();
            fileInfo.setId(item.getFileId());
            fileInfo.setIsDelete(IsDeleteEnum.DELETE.getCode());
            this.sqlManager.updateTemplateById(fileInfo);
        });
        //需新增的文件
        newList.forEach(item -> {
            FileInfo fileInfo = new FileInfo();
            fileInfo.setId(item.getFileId());
            fileInfo.setProjectNo(projectNo);
            fileInfo.setBizType(fileType);
            this.sqlManager.updateTemplateById(fileInfo);
        });
    }

    @Override
    public boolean fileBizTypeChange(List<FileInfoVo> newList, List<String> noList, Integer bizType){
        newList.forEach(item -> {
            FileInfo fileInfo = new FileInfo();
            fileInfo.setId(item.getFileId());
            fileInfo.setProjectNo(String.join(",", noList));
            fileInfo.setBizType(bizType);
            this.sqlManager.updateTemplateById(fileInfo);
        });
        return true;
    }

    @Override
    public boolean deleteFile(List<FileInfoVo> newList){
        newList.forEach(item -> {
            FileInfo fileInfo = new FileInfo();
            fileInfo.setId(item.getFileId());
            fileInfo.setProjectNo("");
            fileInfo.setIsDelete(IsDeleteEnum.DELETE.getCode());
            this.sqlManager.updateTemplateById(fileInfo);
        });
        return true;
    }

    @Override
    public boolean deleteOneProjectNo(List<FileInfoVo> newList,String projectNo){
        newList.forEach(item -> {
            FileInfo fileInfo = this.selectById(item.getFileId());
            List<String> noList =  Arrays.asList(fileInfo.getProjectNo().split(","));
            int index = 0;
            for (int i=0;i<noList.size();i++){
                if (noList.get(i).equals(projectNo)){
                    index = i;
                }
            }
            noList.remove(index);
            fileInfo.setProjectNo(String.join(",", noList));
            this.sqlManager.updateTemplateById(fileInfo);
        });
        return true;
    }

    @Override
    public RtnInfo<PageInfoBT<FileInfo>> pageList(FileInfoDTO fileInfoDTO){
        PageQuery<FileInfo> pageQuery = new PageQuery<>(fileInfoDTO.getOffset(),
                fileInfoDTO.getLimit(), fileInfoDTO);
        PageQuery<FileInfo> result =
                this.sqlManager.pageQuery("fileInfo.selectPageList", FileInfo.class, pageQuery);
        //为空直接返回
        if (CollectionUtils.isEmpty(result.getList())) {
            return RtnInfo.success(new PageInfoBT<>(result));
        }
        result.getList().forEach(item -> {
            UserSingleDetailQueryDTO userSingleDetailQueryDTO = new UserSingleDetailQueryDTO();
            userSingleDetailQueryDTO.setUserId(item.getCreateBy());
            item.setCreateName(adminUserRemoteService.singleDetailQuery(userSingleDetailQueryDTO).getData().getName());
        });
        PageInfoBT<FileInfo> re = new PageInfoBT<>(result);
        rebuild(re);
        return RtnInfo.success(re);
    }

    private void rebuild(PageInfoBT<FileInfo> re){
        List<FileInfo> list = re.getRows();
        if(CollectionUtils.isEmpty(list)){
            re.setTotal(0);
        }
    }

    //项目归档文件处理
    @Override
    public RtnInfo<?> fileArchiveSave(Long fileId,String projectNo){
        FileInfo fileInfo = new FileInfo();
        fileInfo.setId(fileId);
        fileInfo.setBizType(ProjectFileTypeEnum.ARCHIVE.getCode());
        fileInfo.setProjectNo(projectNo);
        return RtnInfo.success(this.updateById(fileInfo));
    }

    @Override
    public RtnInfo<?> fileArchiveDelete(Long fileId){
        FileInfo fileInfo = new FileInfo();
        fileInfo.setId(fileId);
        fileInfo.setProjectNo("");
        fileInfo.setIsDelete(IsDeleteEnum.DELETE.getCode());
        return RtnInfo.success(this.updateById(fileInfo));
    }


}
